package com.gx.admin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.gx.admin.service.CourseService;
import com.gx.admin.service.TeacherService;
import com.student.demo.config.BasicController;
import com.student.demo.entity.Course;
import com.student.demo.entity.Teacher;
import com.student.demo.repository.CourseRepository;

@Controller
//@RestController
@RequestMapping("teacher_gl")
public class TeacherController extends BasicController{

	@Resource
	private TeacherService teacherService;
	@Resource
	private CourseService courseService;
	@RequestMapping("all")
	@ResponseBody
	public Object findAll(Integer page,Map<String, Object>map) {
		List<Teacher> teachers = teacherService.findByPage(page, 10);
		map.put("teachers",teachers);
		return teacherService.findByPage(page, 10);
//		return teachers;
	}
//	@GetMapping("find_all")
////	@ResponseBody
//	public Object findAll(Map<String, Object>map) {
//		List<Teacher> teachers = teacherService.findAll();
//		map.put("teachers",teachers);
////	return teacherService.findByPage(page, 3);
//	return "teachers";
////		return teacherService.findAll();
//	}	
	@GetMapping("delete")
	public String delete(Integer id) {
		teacherService.remove(id);
		return "redirect:all?page=0";
	}
	
	@PostMapping("add")
	@ResponseBody
	public Object add(@Valid Teacher teacher) {
		Teacher addcar = teacherService.add(teacher);
		return teacher;
	}
	
	@PostMapping("addCourse")
	@ResponseBody
	public String addCourse(Integer dId,Integer bId,String bNumber,String bName,Integer bAge,Integer bSex,String bmiaoshu,String bimage ) {
		System.out.println("bNumber"+bNumber);
		System.out.println("bName"+bName);
		System.out.println("bAge"+bAge);
		Course findOne = courseService.findOne(dId);
		Teacher teacher = new Teacher();
		teacher.setbNumber(bNumber);
		teacher.setbName(bName);
		teacher.setbAge(bAge);
		teacher.setbSex(bSex);
		teacher.setBmiaoshu(bmiaoshu);
		teacher.setBimage(bimage);
		teacherService.add(teacher);
		findOne.setTeacher(teacher);
		courseService.add(findOne);
		return "<h1>添加陈宫</h1>";
	}
	@RequestMapping("by_id")
	@ResponseBody
	public Teacher findByBid(Integer id) {
		return teacherService.findBybId(id);
	}
	
	@RequestMapping("by_number")
	@ResponseBody
	public Teacher findBnuber(String bname) {
	return	teacherService.findBnumber(bname);
	}
	
	@RequestMapping("xixi")
	public String haha() {
		return "redirect:http://192.168.1.113/haha/teacher.html";
	}
}